package com.fuluwa.domain.bank.model;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.fuluwa.domain.bank.model.Account.AccountType;
import com.fuluwa.domain.shared.AbstractIdDomainObject;

//Adding currency attribute
/**
 * General account info
 * @author yongxia
 *
 */
@Entity
@Table(name = "b_gl_acct")
public class GLAccount extends AbstractIdDomainObject {

	private Integer glCode;

	@Column(name = "f_bal", columnDefinition = "decimal(13,2)")
	private BigDecimal balance = new BigDecimal(0.00);

	@Column(name = "bal_debit", columnDefinition = "decimal(13,2)")
	private BigDecimal debitBalance = new BigDecimal(0.00);

	@Column(name = "bal_cedit", columnDefinition = "decimal(13,2)")
	private BigDecimal creditBalance = new BigDecimal(0.00);

	// @Embedded
	// private Bank bank = Bank.getDefaultBank();

	@Enumerated(EnumType.STRING)
	private AccountType accountType;
	
	@Enumerated(EnumType.STRING)
	private GeneralLedger generalLedger;

	@Temporal(TemporalType.DATE)
	@Column(name = "dat_post")
	private Date postDate = new Date();

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "tim_post")
	private Date postTime = new Date();

	private GLAccount() {
	}
	

	public GLAccount(Integer glCode, BigDecimal balance, AccountType accountType, boolean isDebit) {
		this(accountType, balance, isDebit);
		this.glCode = glCode;
		generalLedger = GeneralLedger.findByGlCode(glCode);
	}


	public GLAccount(AccountType accountType, BigDecimal balance, boolean isDebit) {
		if (isDebit) {
			this.balance = balance.negate();
			this.debitBalance = balance;
			generalLedger=accountType.libility;
			this.glCode = accountType.libility.getGlCode();
		} else {
			this.balance = balance;
			creditBalance = balance;
			generalLedger=accountType.asset;
			this.glCode = accountType.asset.getGlCode();
		}
		this.accountType = accountType;
	}

	public BigDecimal getBalance() {
		return balance;
	}

	public BigDecimal getDebitBalance() {
		return debitBalance;
	}

	public BigDecimal getCreditBalance() {
		return creditBalance;
	}

	public Date getPostDate() {
		return postDate;
	}

	public Date getPostTime() {
		return postTime;
	}

	// public Bank getBank() {
	// return bank;
	// }

	public Integer getGlCode() {
		return glCode;
	}
	
}
